package com.vay.de.dang.online.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vay.de.dang.online.VnApp;
import com.vay.de.dang.online.resp.RRepaymentMethodDto;
import com.vay.de.dang.online.ui.apply.TakeImageActivity;
import com.vay.de.dang.online.util.VnUtil;
import com.vay.de.dang.online.R;
import com.vay.de.dang.online.entiy.MenuItemEntity;

import java.util.ArrayList;
import java.util.List;

public class DialogVoucher extends DialogBase {

    private LinearLayout imageLayout;
    private Activity activity;
    private DialogCallback mCallback;
    private TextView tvRepayType;
    private List<MenuItemEntity> repayType = new ArrayList<>();
    private MenuItemEntity optionItem;

    public DialogVoucher(Context context) {
        super(context);
        super.setContentView(R.layout.dialog_voucher);
        autoWidth(1.0f);
        activity = (Activity) context;
        imageLayout = findViewById(R.id.image_layout);
        tvRepayType = findViewById(R.id.tv_repay_type);

        findViewById(R.id.bnt_close).setOnClickListener(view -> {
            dismiss();
        });
        findViewById(R.id.bnt_submit).setOnClickListener(view -> {
            if (mCallback != null) {
                mCallback.onConfirm();
            }
        });
        findViewById(R.id.layout_1).setOnClickListener(view -> {
            new DialogMenuOption(activity)
                    .setOnOptionItemClick(bean -> {
                        optionItem = bean;
                        tvRepayType.setText(bean.getLabel());
                    })
                    .showList(VnApp.getStr(R.string.repayment_label_method), repayType);
        });
    }

    public DialogVoucher setCallback(DialogCallback callback) {
        mCallback = callback;
        return this;
    }

    public void showChoice(List<RRepaymentMethodDto> ways) {
        createIcon(R.mipmap.ic_camera_upload, null);
        repayType.clear();
        for (RRepaymentMethodDto item : ways) {
            MenuItemEntity op = new MenuItemEntity();
            op.setLabel(item.getRepaymentValue());
            op.setValue(item.getRepaymentKey());
            repayType.add(op);
        }

        optionItem = repayType.get(0);
        tvRepayType.setText(optionItem.getLabel());

        show();
    }

    public MenuItemEntity getOptionItem() {
        return optionItem;
    }

    public void loadImage(String path) {
        if (VnUtil.isEmpty(path)) return;
        createIcon(R.mipmap.ic_camera_upload, path);
    }

    private void createIcon(int rid, String path) {

        int num = 3;
        int p = VnApp.getDimension(R.dimen.x16);
        int p2 = VnApp.getDimension(R.dimen.x12);
        int w = (VnApp.getScreenWidth() - p * 2 - (num - 1) * p2) / num;

        imageLayout.removeAllViews();
        ImageView icon = new ImageView(activity);
        if (!VnUtil.isEmpty(path)) {
            Bitmap src = BitmapFactory.decodeFile(path);
            icon.setImageBitmap(src);
            icon.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            icon.setImageResource(rid);
        }

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(w, w);
        lp.bottomMargin = VnApp.getDimension(R.dimen.x8);
        imageLayout.addView(icon, lp);
        icon.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setClass(activity, TakeImageActivity.class);
            intent.putExtra("fromType", 2);
            activity.startActivityForResult(intent, 3000);
        });
    }
}
